package com.peach.controller;

import com.peach.pojo.News;
import com.peach.pojo.User;
import com.peach.service.NewsService;
import com.peach.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class testController {

    @Autowired
    private NewsService newsService;

    @Autowired
    private UserService userService;

    @RequestMapping("/index")
    public String test(Model model){
        return "index";
    }
    
    @GetMapping("/index2")
    public String toIndex2(Model model){
        List<News> news1 = newsService.selectNewsByTypeIdReturnNews(1);
        List<News> news2 = newsService.selectNewsByTypeIdReturnNews(2);
        List<News> news3 = newsService.selectNewsByTypeIdReturnNews(3);
        model.addAttribute("news1",news1);
        model.addAttribute("news2",news2);
        model.addAttribute("news3",news3);
        return "index2";
    }

    @GetMapping("/login2")
    public String login2(){
        return "/login";
    }

    @PostMapping("/login")
    public String login(User user, Model model, HttpServletRequest request){
        Integer userId = user.getUserId();
        String userPwd = user.getUserPwd();
        User user1 = userService.selectOneUser(userId);
        System.out.println("??");
        System.out.println(userId+"   "+userPwd+"  "+user1);
        if(user1!=null && userPwd.equals(user1.getUserPwd())){
            request.getSession().setAttribute("userGmx",user1.getUserName());
            return "index";
        }else{
            model.addAttribute("msg","用户名或密码错误！");
            return "/login";
        }
    }

    @GetMapping("/logout")
    public String logout(HttpServletRequest request){
        request.getSession().removeAttribute("userGmx");
        return "/login";
    }


}
